import 'package:cult/function/widgets/login_dialog_widget.dart';
import 'package:get/get.dart';
import '../../function/model/post_model.dart';
import '../../function/model/user_model.dart';
import '../../function/state/post_state.dart';
import '../../function/state/user_state.dart';
import '../../function/state/like_state.dart';

/// 植物种植活动挑战页面控制器
class PlantChallengePageController extends GetxController {
  final PostState _postState = Get.find<PostState>();
  final UserState _userState = Get.find<UserState>();
  final LikeState _likeState = Get.find<LikeState>();

  // 当前选中的分类索引
  final RxInt _currentCategoryIndex = 0.obs;

  // 轮播图控制器
  final RxInt _carouselIndex = 0.obs;

  @override
  void onInit() {
    super.onInit();
    // 初始化页面数据
    _initializePageData();
  }

  /// 初始化页面数据
  void _initializePageData() {
    // 只有在用户已登录时才设置用户ID
    if (_userState.isLoggedIn && _userState.currentUser != null) {
      _likeState.setCurrentUserId(_userState.currentUser!.id);
    }
  }

  /// 更新用户相关的依赖状态
  void _updateUserDependencies() {
    if (_userState.isLoggedIn && _userState.currentUser != null) {
      _likeState.setCurrentUserId(_userState.currentUser!.id);
    } else {
      // 用户登出时清空用户ID
      _likeState.setCurrentUserId('');
    }
  }

  /// 获取所有分类
  List<PostCategory> get categories => PostCategory.values;

  /// 获取当前选中的分类
  PostCategory get currentCategory => categories[_currentCategoryIndex.value];

  /// 获取当前轮播图索引
  int get carouselIndex => _carouselIndex.value;

  /// 切换分类
  void changeCategory(int index) {
    _currentCategoryIndex.value = index;
    _carouselIndex.value = index;
  }

  /// 轮播图页面改变
  void onCarouselPageChanged(int index) {
    _carouselIndex.value = index;
    _currentCategoryIndex.value = index;
  }

  /// 获取当前分类下的帖子
  List<PostModel> get currentCategoryPosts {
    return _postState.allPosts
        .where((post) => post.category == currentCategory)
        .toList();
  }

  /// 获取用户头像
  String getUserAvatar(String userId) {
    final user = _userState.getUserById(userId);
    return user?.avatar ?? 'assets/images/avatar1.png';
  }

  /// 获取用户名
  String getUserName(String userId) {
    final user = _userState.getUserById(userId);
    return user?.nickname ?? user?.username ?? 'UnknownUser';
  }

  /// 获取用户信息
  UserModel? getUserInfo(String userId) {
    return _userState.getUserById(userId);
  }

  /// 切换帖子点赞状态
  void togglePostLike(String postId) {
    // 更新用户依赖状态
    _updateUserDependencies();

    // 检查用户是否已登录
    if (!_userState.isLoggedIn) {
      // 用户未登录，可以显示提示或跳转到登录页
      LoginDialogUtils.showLoginDialog();
      return;
    }

    // 用户已登录，根据点赞状态进行切换
    if (_likeState.isPostLiked(postId)) {
      _likeState.unlikePost(postId);
    } else {
      _likeState.likePost(postId);
    }
  }

  /// 检查帖子是否被点赞
  bool isPostLiked(String postId) {
    // 只有登录用户才能点赞
    if (!_userState.isLoggedIn) {
      return false;
    }
    return _likeState.isPostLiked(postId);
  }

  /// 获取帖子点赞数
  int getPostLikes(String postId) {
    return _likeState.getPostLikeCount(postId);
  }
}
